package com.myt.matp.core;

import com.myt.matp.utils.Edit;

import java.util.Vector;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

public final class FontSystem {

    public static final int TYPE_JME = 1;
    public static final int TYPE_MAT = 2;

    public static final int FACE_SYSTEM = Font.FACE_SYSTEM;
    public static final int FACE_MONOSPACE =  Font.FACE_MONOSPACE;
    public static final int FACE_PROPORTIONAL = Font.FACE_PROPORTIONAL;
    public static final int STYLE_PLAIN = Font.STYLE_PLAIN;
    public static final int STYLE_BOLD = Font.STYLE_BOLD;
    public static final int STYLE_ITALIC = Font.STYLE_ITALIC;
    public static final int STYLE_UNDERLINED = Font.STYLE_UNDERLINED;
    public static final int SIZE_SMALL = Font.SIZE_SMALL;
    public static final int SIZE_MEDIUM = Font.SIZE_MEDIUM;
    public static final int SIZE_LARGE = Font.SIZE_LARGE;

    private static final int INIT_SIZE = 2;

    private int m_Type = FontSystem.TYPE_JME;

    private Vector m_Fonts = null;
    private Object m_CurrentFont = null;

    public FontSystem(int type) {
	m_Type = (type == TYPE_MAT) ? TYPE_MAT : TYPE_JME;
	m_Fonts = new Vector(INIT_SIZE);
    }

    public void setFont(int face, int style, int size) {
	switch (m_Type) {
	case TYPE_MAT:
	    m_CurrentFont = createFontMAT(face, style, size);
	    break;
	default:
	    m_CurrentFont = createFontJME(face, style, size);
	    break;
	}

    }

    public int getFontHeight() {
	int ret = 0;
	switch (m_Type) {
	case TYPE_MAT:
	    ret = getFontHeightMAT();
	    break;
	default:
	    ret = getFontHeightJME();
	    break;
	}
	return ret;
    }

    private int getFontHeightMAT() {
	return 0;
    }

    private int getFontHeightJME() {
	return (((Font)m_CurrentFont).getHeight());
    }

    private MFont createFontMAT(int face, int style, int size) {
	return null;
    }

    private MFont searchFontMAT(int face, int style, int size) {
	return null;
    }

    private Font createFontJME(int face, int style, int size) {
	Font f = searchFontJME(face, style, size);
	if (f == null) {
	    f = Font.getFont(face, style, size);
	    m_Fonts.addElement(f);
	}
	return f;
    }

    private Font searchFontJME(int face, int style, int size) {
	Font f = null;
	for (int i = 0; i < m_Fonts.size(); i++) {
	    f = (Font)m_Fonts.elementAt(i);
	    if ((f.getFace() == face) && 
		(f.getStyle() == style) &&
		(f.getSize() == size)) {		
		break;
	    }
	    f = null;
	}
	return f;
    }

    public void drawChar(Graphics g, char character, int x, int y, int anchor) {
	if (m_CurrentFont != null) {
	    switch (m_Type) {
	    case FontSystem.TYPE_MAT:
		drawCharMAT(g, character, x, y, anchor);
		break;
	    default:
		drawCharJME(g, character, x, y, anchor);
		break;
	    }
	}
    }

    private void drawCharMAT(Graphics g, char character, int x, int y, int anchor) {
	MFont f = (MFont)m_CurrentFont;
    }

    private void drawCharJME(Graphics g, char character, int x, int y, int anchor) {
	g.setFont((Font)m_CurrentFont);
	g.drawChar(character, x, y, anchor);
    }

    public int charWidth(char ch) {
	int ret = 0;
	if (m_CurrentFont != null) {
	    switch (m_Type) {
	    case TYPE_MAT:
		ret = charWidthMAT(ch);
		break;
	    default:
		ret = charWidthJME(ch);
		break;
	    }
	}
	return ret;
    }

    public int charsWidth(Edit editor, int start, int length) {
	int point;
	int ret = 0;

 	//#ifdef DEBUG_FONTSYSTEM
	//# System.out.println(getClass().getName() + " {");
	//#endif

	point = editor.getPoint();

 	//#ifdef DEBUG_FONTSYSTEM
	//# System.out.println("\tstart = " + start);
	//# System.out.println("\tlength = " + length);
	//#endif

	editor.setPoint(start);
	//#ifdef DEBUG_FONTSYSTEM
	//# System.out.print("\t");
	//#endif
	while (length > 0) {
	    //#ifdef DEBUG_FONTSYSTEM
	    //# System.out.print(editor.getChar());
	    //#endif
	    ret += charWidth(editor.getChar());
	    editor.movePoint(1);
	    length--;
	}
	//#ifdef DEBUG_FONTSYSTEM
	//# System.out.print("\n");
	//#endif

	editor.setPoint(point);

 	//#ifdef DEBUG_FONTSYSTEM
	//# System.out.println("}");
	//#endif
	return ret;
    }

    private int charWidthMAT(char ch) {
	return 0;
    }

    private int charWidthJME(char ch) {
	return ((Font)m_CurrentFont).charWidth(ch);
    }

}